package com.kuanter.kuanterauto.activity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.baseui.BaseActivity_NoBar;
import com.kuanter.kuanterauto.entity.MessageInfo;
import com.kuanter.kuanterauto.main.HomeActivityGroup;
import com.kuanter.kuanterauto.main.MainActivity;
import com.kuanter.kuanterauto.utils.SharePrefenceUtil;
/** 
 * @author utopia
 * 
 */

public class GuideActivity extends BaseActivity_NoBar implements OnPageChangeListener {
	ViewPager whatsnew_viewpager;
//	PagerTitleStrip pagertitle;
	
	ImageView page0;
	ImageView page1;
	ImageView page2;
	
	private ImageView one,two;
	
	private int currIndex = 0;//当前页面  
	
	Button startBtn;
	
	List<MessageInfo> messageInfos;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guide);
		Intent intent = getIntent();
		messageInfos = (List<MessageInfo>) intent.getSerializableExtra("messageInfos");
		initView();
	}
	
	public void start(View view) {
		SharePrefenceUtil.setLoad(this, true);
		String city = SharePrefenceUtil.getCity(GuideActivity.this);
//		Bundle city_bundle = new Bundle();
//		city_bundle.putString("city", city);
		//首先判断当前城市有没有选择，没选择就跳转到城市列表
		if(city.equals("")) {
			qStartActivity(CityListActivity.class, null);
			finish();
			startActivity_anim(0);
		} else {
			if (MainActivity.ID != 0) {
				// try {
				// LoadGRZXData(ID);
				// } catch (Exception e) {
				// e.printStackTrace();
				// }
				// if (carinfos != null && consumptions != null) {
				// Bundle b = new Bundle();
				// b.putSerializable(CardInfoList.TAG,
				// (Serializable) carinfos);
				// b.putSerializable(ConsumptionList.TAG,
				// (Serializable) consumptions);
				// b.putSerializable(VehicleInfoList.TAG,
				// (Serializable) vehicleInfos);
				// qStartActivity(HomeActivityGroup.class, b);
				// finish();
				// startActivity_anim(0);
				//
				// } else {
				if (messageInfos != null) {
					Bundle bundle = new Bundle();
					bundle.putSerializable(MessageInfo.TAG,
							(Serializable) messageInfos);
					qStartActivity(HomeActivityGroup.class, bundle);
					finish();
					startActivity_anim(0);
				} else {
					qStartActivity(HomeActivityGroup.class, null);
					finish();
					startActivity_anim(0);
				}
				// }
			} else {
				if (messageInfos != null) {
					Bundle bundle = new Bundle();
					bundle.putSerializable(MessageInfo.TAG,
							(Serializable) messageInfos);
					qStartActivity(HomeActivityGroup.class, bundle);
					finish();
					startActivity_anim(0);
				} else {
					qStartActivity(HomeActivityGroup.class, null);
					finish();
					startActivity_anim(0);
				}
			}
		}
		
	}

	List<View> list;
	public void initView() {
		whatsnew_viewpager = (ViewPager) findViewById(R.id.whatsnew_viewpager);
//		pagertitle = (PagerTitleStrip) findViewById(R.id.pagertitle);
		
		page0 = (ImageView) findViewById(R.id.page0);
		page1 = (ImageView) findViewById(R.id.page1);
		page2 = (ImageView) findViewById(R.id.page2);
		whatsnew_viewpager.setOnPageChangeListener(this);
		
		list = new ArrayList<View>();
		one = new ImageView(this);
		LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		one.setLayoutParams(lp);
		one.setScaleType(ScaleType.FIT_XY);
		one.setImageResource(R.drawable.whats_one);
		list.add(one);
		
		two = new ImageView(this);
		two.setImageResource(R.drawable.whats_two);
		two.setLayoutParams(lp);
		two.setScaleType(ScaleType.FIT_XY);
		list.add(two);
		
		list.add(LayoutInflater.from(this).inflate(R.layout.three, null));
		
		PagerAdapter adapter = new PagerAdapter() {
			
			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {
				return arg0 == arg1;
			}
			
			@Override
			public int getCount() {
				return list.size();
			}

			@Override
			public void destroyItem(ViewGroup container, int position,
					Object object) {
				((ViewPager)container).removeView(list.get(position));
			}

			@Override
			public Object instantiateItem(View container, int position) {
				((ViewPager)container).addView(list.get(position));  
				return list.get(position);
			}
			
			
		};
		
		whatsnew_viewpager.setAdapter(adapter);
		
	}


	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onPageSelected(int arg0) {
		Animation animation = null;
		switch (arg0) {
		case 0:
			page0.setImageResource(R.drawable.page_indicator_focused);
			page1.setImageResource(R.drawable.page_indicator_unfocused);
			page2.setImageResource(R.drawable.page_indicator_unfocused);
			if(currIndex == arg0 + 1) {
				animation = new TranslateAnimation(arg0 + 1, arg0, 0, 0);
			}
			break;

		case 1:
			page0.setImageResource(R.drawable.page_indicator_unfocused);
			page1.setImageResource(R.drawable.page_indicator_focused);
			page2.setImageResource(R.drawable.page_indicator_unfocused);
			if(currIndex == arg0 - 1) {
				animation = new TranslateAnimation(arg0-1, arg0, 0, 0);
			} else if(currIndex == arg0 + 1) {
				animation = new TranslateAnimation(arg0+1, arg0, 0, 0);  
			}
			break;
			
		case 2:
			page0.setImageResource(R.drawable.page_indicator_unfocused);
			page1.setImageResource(R.drawable.page_indicator_unfocused);
			page2.setImageResource(R.drawable.page_indicator_focused);
			if (currIndex == arg0-1) {  
                animation = new TranslateAnimation(arg0-1, arg0, 0, 0);  
            }
			break;
		}
		currIndex = arg0;
		animation.setFillAfter(true);
		animation.setDuration(300);
	}
	
	
}

